package com.zui.entity.party;

import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

import com.zui.entity.BaseAutoIdEntity;
import com.zui.entity.setup.ContactMech;


@Entity
@Table(name="party_contact_mech",
		uniqueConstraints={@UniqueConstraint(name="uc_party_contact_mech",
				columnNames={"partyId", "contactMechId"})})
/**
 * Party和ContactMech的中间表，Party单项访问。
 * @TODO: 还要不要继承BaseEntiy从而得到id? updateUser和updateDate还是需要的。
 */
public class PartyContactMech extends BaseAutoIdEntity {
	private static final long serialVersionUID = 1L;
	
	private Party party;
	private ContactMech contactMech;
	private Date fromDate;
	private Date thruDate;
	private String allowSolicitation;
	private String extension;
	private String verified;
	private String comments;
	private Double yearsWithContactMech;
	private Double monthsWithContactMech;
	
	private Set<PartyContactMechPurpose> partyContactMechPurposes = new LinkedHashSet<PartyContactMechPurpose>();

	@Transient
	public void addPartyContactMechPurpose(PartyContactMechPurpose partyContactMechPurpose) {
		partyContactMechPurpose.setPartyContactMech(this);
		if(partyContactMechPurposes == null)
			partyContactMechPurposes = new LinkedHashSet<PartyContactMechPurpose>();
		partyContactMechPurposes.add(partyContactMechPurpose);
	}
	@Transient
	public void removePartyContactMechPurpose(PartyContactMechPurpose partyContactMechPurpose) {
		if(partyContactMechPurpose == null)
			return;
		partyContactMechPurposes.remove(partyContactMechPurposes);
		partyContactMechPurpose.setPartyContactMech(null);
	}
	/*------------------------------
	 * getters and annotations
	 --------------------------------*/
	@ManyToOne() @JoinColumn(name="partyId")
	public Party getParty() {return party;}
	
	@OneToOne(cascade={CascadeType.ALL})
	@JoinColumn(name="contactMechId")
	public ContactMech getContactMech() {return contactMech;}
	
	@OneToMany(mappedBy = "partyContactMech",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
	public Set<PartyContactMechPurpose> getPartyContactMechPurposes() {
		return partyContactMechPurposes;
	}

	public Date getFromDate() {return fromDate;}
	public Date getThruDate() {return thruDate;}

	@Column(length=1)
	public String getAllowSolicitation() {return allowSolicitation;}

	@Column(length=10)
	public String getExtension() {return extension;}

	@Column(length=1)
	public String getVerified() {return verified;}

	@Column(length=500)
	public String getComments() {return comments;}
	public Double getYearsWithContactMech() {return yearsWithContactMech;}
	public Double getMonthsWithContactMech() {return monthsWithContactMech;}

	/*------------------------------
	 * setters
	 --------------------------------*/
	public void setContactMech(ContactMech contactMech) {
		this.contactMech = contactMech;
	}

	public void setParty(Party party) {
		this.party = party;
	}

	public void setFromDate(Date fromDate) {
		this.fromDate = fromDate;
	}

	public void setThruDate(Date thruDate) {
		this.thruDate = thruDate;
	}

	public void setAllowSolicitation(String allowSolicitation) {
		this.allowSolicitation = allowSolicitation;
	}

	public void setExtension(String extension) {
		this.extension = extension;
	}

	public void setVerified(String verified) {
		this.verified = verified;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	public void setYearsWithContactMech(Double yearsWithContactMech) {
		this.yearsWithContactMech = yearsWithContactMech;
	}

	public void setMonthsWithContactMech(Double monthsWithContactMech) {
		this.monthsWithContactMech = monthsWithContactMech;
	}

	public void setPartyContactMechPurposes(
			Set<PartyContactMechPurpose> partyContactMechPurposes) {
		this.partyContactMechPurposes = partyContactMechPurposes;
	}
	
	
	
}
